$(function() {
  $('body').css({
    'height':$(window).height()+'px',
    'overflow':'hidden'
  });

  // 小糊涂文字转语音
  var answer_spd=5;
  $(document).on('tap','.chat_audio',function() {
    var answer_src = $(this).parent().text();
    $('.voice').attr({'src':'http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd='+answer_spd+'&text='+answer_src});
  });

  $('.ui-icon-check').on('tap',function() {
    answer_spd = $('#points').val();
  });

  // 更换头像
  var admin_img='img/admin.jpg',robot_img='img/head.png';
  $('.admin_head').on('tap',function() {
    admin_img=$(this).attr('src');
    $('.admin_head').css({
      'transform':'scale(0.7)'
    });
    $(this).css({
      'transform':'scale(1)'
    });
    $('.admin').attr({'src':admin_img});
  });

  $('.robot_head').on('tap',function() {
    robot_img=$(this).attr('src');
    $('.robot_head').css({
      'transform':'scale(0.7)'
    });
    $(this).css({
      'transform':'scale(1)'
    });
    $('.robot').attr({'src':robot_img});
  });

  //聊天内容
  $('.sentence_btn').on('click',function() {
    if($('.sentence_input').val()!='') {
      var chatval = $('.sentence_input').val();
      $('.sentence_input').val('');
      var uid = new Date().getTime();
      $.ajax({
        // url:'http://op.juhe.cn/robot/index?info='+chatval+'&dtype=json&loc=&userid=&key=00f7db7b99624a54f5ccebf692344a1b',
        url:'http://www.tuling123.com/openapi/api?info='+chatval+'&dtype=json&loc=&userid='+uid+'&key=472a7914683c4f029c50993127673189',
        // url:'json/demo1.json',
        // url:'json/demo2.json',
        type:'post',
        datatype:'json',
        success:function(data) {
          var answer = data.text;
          // 主人说的话
          $('.talking_content').append('<tr><td class="chat_admin"><span>'+chatval+'<a class="ui-btn ui-corner-all ui-icon-audio ui-btn-icon-notext chat_audio"></a></span><img class="admin" src="'+admin_img+'"></td></tr>');

          if(data.url) {
            $('.talking_content').append('<tr><td class="chat_robot"><img class="robot" src="'+robot_img+'"><span>'+answer+'<a class="ui-btn ui-corner-all ui-icon-audio ui-btn-icon-notext chat_audio"></a><br><a href="#pageones" data-transition="slideup" class="detailurl" framesrc="'+data.url+'">点击进入</a></span></td></tr>');
          }

          else {
            $('.talking_content').append('<tr><td class="chat_robot"><img src="img/head.png"><span>'+answer+'<a class="ui-btn ui-corner-all ui-icon-audio ui-btn-icon-notext chat_audio"></a></span></td></tr>');
          }
          $('.saying').scrollTop($('.saying')[0].scrollHeight);
        },
        error:function() {}
      })
    }
  });

  // 详细链接
  $(document).on('tap','.detailurl',function() {
    $('.search_content').attr({'src':$(this).attr('framesrc')});
  })

  // 记事本（添加）
  $('.ui-icon-plus').on('click',function() {
    $('.note').val('');
    var time = new Date();
    var years = time.getFullYear();
    var months = time.getMonth() + 1;
    var days = time.getDate();
    if(months<10) {
      months= '0'+months.toString()
    }
    if(days<10) {
      days= '0'+days.toString()
    }
    var times = years+"-"+months+"-"+days;
    $('.note_date').val(times);
  });

  // 记事本（读取缓存）
  if (localStorage.length!=0) {
    for(var i=0;i<localStorage.length;i++) {
      $('.note_content').append(localStorage.getItem(localStorage.key(i)));
    }
  }
  $('.note_clear').on('tap',function() {
    localStorage.clear();
    $('.note_content').empty();
  })

  // 记事本（保存）
  $('.save').on('click',function() {
    var record_time = $('.note_date').val();
    record_time = record_time.replace('-','年');
    record_time = record_time.replace('-','月');
    record_time += '日';
    if($('.note').val().length!=0) {
      var keys = new Date().getTime();
      var note_app = '<div id="'+keys+'" data-role="collapsible"><h3>'+record_time+'</h3><p>'+$('.note').val()+'</p><a class="ui-btn ui-corner-all ui-icon-audio ui-btn-icon-notext note_audio"></a><a class="ui-btn ui-corner-all ui-icon-delete ui-btn-icon-notext note_delete" del="'+keys+'">图标</a><a href="#pagethrees" class="ui-btn ui-corner-all ui-icon-edit ui-btn-icon-notext note_edit" edi="'+keys+'">图标</a><div>';
      $('.note_content').append(note_app);
      $(".note_content").trigger("create");
      localStorage.setItem(keys,note_app);
    }
    else {
      alert('请输入记录内容');
      $(this).removeAttr('href');
    }
  });

  // 记事本文字转语音
  var note_spd=5;
  $(document).on('tap','.note_audio',function() {
    var note_src = $(this).parent().children('p').text();
    $('.voice').attr({'src':'http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd='+note_spd+'&text='+note_src});
  });

  // 记事本（删除）
  $(document).on('tap','.note_delete',function() {
    localStorage.removeItem($(this).attr('del'));
    $('#'+$(this).attr('del')).remove();
  });

  // 记事本（修改）
  var edit_id;
  $(document).on('tap','.note_edit',function() {
    edit_id = $(this).attr('edi');
    var edit_time = $('#'+edit_id).children('h3').children().html();
    var edit_content = $('#'+edit_id).children(':last').children('p').text();
    edit_time = edit_time.substring(0, edit_time.indexOf('<')-1)
    edit_time = edit_time.replace('年','-');
    edit_time = edit_time.replace('月','-');
    $('.notes').val(edit_content);
    $('.note_dates').val(edit_time);
  });

$('.saves').on('click',function() {
    var record_time = $('.note_dates').val();
    record_time = record_time.replace('-','年');
    record_time = record_time.replace('-','月');
    record_time += '日';
    if($('.notes').val().length!=0) {
      $('#'+edit_id).remove();
      var note_app = '<div id="'+edit_id+'" data-role="collapsible"><h3>'+record_time+'</h3><p>'+$('.notes').val()+'</p><a class="ui-btn ui-corner-all ui-icon-audio ui-btn-icon-notext note_audio"></a><a class="ui-btn ui-corner-all ui-icon-delete ui-btn-icon-notext note_delete" del="'+edit_id+'">图标</a><a href="#pagethrees" class="ui-btn ui-corner-all ui-icon-edit ui-btn-icon-notext note_edit" edi="'+edit_id+'">图标</a><div>';
      $('.note_content').append(note_app);
      $(".note_content").trigger("create");
      localStorage.removeItem(edit_id);
      localStorage.setItem(edit_id,note_app);
    }
    else {
      alert('请输入记录内容');
      $(this).removeAttr('href');
    }
  });
})
